import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/bean/histroy_list_entity.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:jyofservice/util/AesUtil.dart';

class HistroyPage extends StatefulWidget {
  final String? sfbm;
  final String? xqid;
  HistroyPage({this.sfbm, this.xqid});

  @override
  _HistroyPageState createState() => _HistroyPageState();
}

class _HistroyPageState extends State<HistroyPage> {
  List<HistroyListDataData> histroyListData = [];
  ScrollController scrollController = ScrollController();
  int page = 1;
  bool isloading = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getHistroyListData();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        print('滑动到了最底部');
        page++;
        getHistroyListData();
      }
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0.5,
          centerTitle: true,
          title: Text("历史工单",
              style: TextStyle(
                  color: Colors.white, fontSize: ScreenUtil().setSp(45))),
        ),
      body: Container(
        color: const Color(0xFFF2F5F9),
        child:Stack(
          alignment: Alignment(0.5, 0.5),
          children: <Widget>[
            RefreshIndicator(
                child: ListView(

              physics: AlwaysScrollableScrollPhysics(),
                  controller: scrollController,
                  children: <Widget>[
                    SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child:  DataTable(
                        columnSpacing: 20,
                        horizontalMargin: 20,
                        columns: [

                          DataColumn(
                              label: Text(
                                '住户地址',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),
                          DataColumn(
                              label: Text(
                                '来电号码',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),
                          DataColumn(
                              label: Text(
                                '工单描述',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),

                          DataColumn(
                              label: Text(
                                '处理人员',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),
                          DataColumn(
                              label: Text(
                                '处理时间',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),
                          DataColumn(
                              label: Text(
                                '处理结果',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),
                          DataColumn(
                              label: Text(
                                '处理内容',
                                style: TextStyle(fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold,color: Colors.blue),
                              )),

                        ],
                        rows: getTableRow(),
                        //为用户提供行数选择
                      ),
                    )
                  ],
                ),
                onRefresh: onHeaderRefresh),
            Offstage(
              offstage: isloading,
              child: SpinKitFadingCircle(
                color: Colors.blue,
                size: 50.0,
              ),
            )
          ],
        )

      ),

    );
  }

  //刷新
  Future<Null> onHeaderRefresh() async {
    page = 1;
    histroyListData.clear();
    getHistroyListData();
  }

  List<DataRow> getTableRow() {
    List<DataRow> list = [];
    for (int i = 0; i < histroyListData.length; i++) {
      list.add(DataRow(cells: [
        DataCell(Text(
          histroyListData[i].allName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].comePhone??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].describe??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLUserName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLTime??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].resultName??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),
        DataCell(Text(
          histroyListData[i].cLRemarks??"",
          style: TextStyle(fontSize: ScreenUtil().setSp(35)),
        )),

      ]));
    }
    print(list.length);

    return list;
  }

  //获取历史列表数据
  Future getHistroyListData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    print("历史工单key===$key==${widget.sfbm}");

    var where = "";
    if (widget.sfbm != null&&widget.sfbm!.length!=0) {
      where = " Sfbm='" + widget.sfbm! + "'";
    } else {
      where = " All_name='" + widget.xqid! + "'";
    }
    String aesData= await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "view": "vw_GD_history",
      "wherestr": where,
      "limit": 10,
      "page": page,
      "sortName": "CreateDate",
      "sortOrder": "desc"
      ,"ClientType":"phone","ClientKey":aesData
    };
    print('历史'+data.toString());

    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/postrowdataforlayui",
        data: data);

    print("${WebConfig().baseUrl + "DB/postrowdataforlayui"}");
    print("历史工单:${response}==$data");

    if (response.data["code"] == 0) {
      for (int i = 0; i < response.data["data"].length; i++) {
        HistroyListDataData diaoDuListEntity =
            new HistroyListDataData.fromJson(response.data["data"][i]);
        histroyListData.add(diaoDuListEntity);
      }
      isloading= true;
      if (mounted) {
        setState(() {});
      }
    } else {
      Fluttertoast.showToast(msg: "当前请求数据错误请重新刷新");
    }
  }
}
